package com.spottydog.virtues;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.spottydog.virtues.VirtueDate.Virtue;

public class ChartActivity extends Activity {
	
	private VirtueDatabase db;
	private VirtueChart chart;
	
	private Date currentDate;
	private Virtue currentVirtue;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
    	
		getWindow().setBackgroundDrawableResource(R.drawable.paper);
		setContentView(R.layout.chart);
		
		Typeface face = Typeface.createFromAsset(getAssets(), "fonts/BenjaminFranklin.ttf");
		((TextView) findViewById(R.id.chart_period_prompt)).setTypeface(face);
		((TextView) findViewById(R.id.chart_virtue_prompt)).setTypeface(face);
		
		this.db = new VirtueDatabase(this);
		
		final Spinner chartVirtue = (Spinner) findViewById(R.id.chart_virtue);
		ArrayAdapter<CharSequence> virtueAdapter = ArrayAdapter.createFromResource(this, R.array.chart_virtues_array,
				android.R.layout.simple_spinner_item);
		virtueAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	chartVirtue.setAdapter(virtueAdapter);
    	
    	chartVirtue.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapter, View v,
					int position, long id) {
				String selected = adapter.getItemAtPosition(position).toString();
				
				if (selected.equals("All virtues")) currentVirtue = null;
				else currentVirtue = Virtue.valueOf(selected);
				
				populateChartData();
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {
				return;
			}
		});
		
		final Spinner chartPeriod = (Spinner) findViewById(R.id.chart_period);
		ArrayAdapter<CharSequence> periodAdapter = ArrayAdapter.createFromResource(this, R.array.chart_periods_array,
				android.R.layout.simple_spinner_item);
		periodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	chartPeriod.setAdapter(periodAdapter);
    	
    	chartPeriod.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapter, View v,
					int position, long id) {
				Calendar c = Calendar.getInstance();
				String selected = adapter.getItemAtPosition(position).toString();
				
				if (selected.equals("Last 2 weeks")) c.add(Calendar.DATE, -14);
				else if (selected.equals("Last month")) c.add(Calendar.MONTH, -1);
				else if (selected.equals("Last 2 months")) c.add(Calendar.MONTH, -2);
				
				currentDate = c.getTime();
				populateChartData();
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {
				return;
			}
		});
		
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DATE, -14);
    	currentDate = cal.getTime();
    	currentVirtue = null;
    	
    	this.chart = new VirtueChart(this);
    		
    	((FrameLayout) findViewById(R.id.chart_frame)).addView(this.chart);
    	populateChartData();
	}
	
	private void populateChartData() {
		SortedMap<Date, Integer> chartData = new TreeMap<Date, Integer>();
		
		this.currentDate.setHours(0);
		this.currentDate.setMinutes(0);
    	this.currentDate.setSeconds(0);
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(this.currentDate);
		
		Calendar now = Calendar.getInstance();
		
		while (cal.before(now)) {
			Integer count = 0;
			List<VirtueDate> vds = this.db.getForDate(cal.getTime());
			for (VirtueDate vd : vds) {
				if (this.currentVirtue == null || this.currentVirtue == vd.getVirtue()) count += vd.getCount();
			}
			chartData.put(cal.getTime(), count);
			cal.add(Calendar.DATE, 1);
		}
		
		this.chart.setChartData(chartData);
		this.chart.invalidate();
	}
}
